##################################################################################################
#Ms: The Impact of Local Ceasefires: Evidence from the Syrian Civil War
#Authors: Magnus Lundgren, Isak Svensson, and Dogukan Karakus
#Date: August 9, 2022
#Software: R
#Hardware: Mac
#Data available via Harvard Dataverse
##################################################################################################

# Load required packages

library(lme4)
library(stargazer)
library(geosphere)
library(dplyr)


# Figure 2 

temp<-read.csv("~/Downloads/daraa_data.csv")
temp2<-read.csv("~/Downloads/barzeh_data.csv")

par(mfrow=c(1,2))
plot(as.Date(temp$date),temp$X,type="l",xlab="",ylab="Fatalities",main="Daraa, July 2017")
abline(h=0)
abline(v=as.Date("2017-07-09"),col="red",lwd=2)
plot(as.Date(temp2$date),temp2$X,type="l",xlab="",ylab="Fatalities",main="Barzeh, May 2014")
abline(h=0)
abline(v=as.Date("2014-05-01"),col="red",lwd=2)


# Figure 3

data<-read.csv("~/Downloads/summary_fit_data.csv")

par(mfrow=c(1,3))

data<-data[order(data$ols),]
plot(1:length(data[,1]),data$ols,ylim=c(-55,55),pch=16,xaxt="n",xlab="",ylab="Segmented OLS estimate", main="Immediate")
for (i in 1:length(data[,1])){
  segments(i,(data$ols[i]+data$ols_sd[i]*1.5),i,(data$ols[i]-data$ols_sd[i]*1.5))
}
abline(h=0,lty=3,) 

data<-data[order(data$fit0),]
plot(1:length(data[,1]),data$fit0,ylim=c(-15,15),pch=16,xaxt="n",xlab="",ylab="ARIMA estimate", main="Agreement period")
for (i in 1:length(data[,1])){
  segments(i,(data$fit0[i]+data$sd0[i]*1.5),i,(data$fit0[i]-data$sd0[i]*1.5))
}
abline(h=0,lty=3,)

data<-data[order(data$fit1),]
plot(1:length(data[,1]),data$fit1,ylim=c(-20,15),pch=16,xaxt="n",xlab="",ylab="ARIMA estimate", main="Long-term")
for (i in 1:length(data[,1])){
  segments(i,(data$fit1[i]+data$sd1[i]*1.5),i,(data$fit1[i]-data$sd1[i]*1.5))
}
abline(h=0,lty=3,) 


# Table III (Models take a very long time to estimate)

rm(list=ls()) # clear memory
data_all<-read.csv("~/Downloads/syria_data_all.csv")

model1 <- glmer.nb(fatalities~cf +fatalities_lag+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model2 <- glmer.nb(fatalities~cf + prev_cf+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model3 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model4 <- glmer.nb(fatalities~cf +  prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+cf_sl_univ+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model5 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all[data_all$date<"2015-01-01",],verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model6 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all[data_all$date>"2015-01-01",],verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

stargazer(model1,model2,model3,model4,model5, model6,type="text",df=F, digits=2,star.cutoffs=c(0.05,0.01,0.001))


# Table IV

model7 <- glmer.nb(fatalities~cf + prev_cf+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+government+ (1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model8 <- glmer.nb(fatalities~cf + prev_cf+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+government+government:prev_cf+ (1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

stargazer(model7,model8, type="text",df=F, digits=2,star.cutoffs=c(0.05,0.01,0.001))


# Online appendix

# Table A1

rm(list=ls())
  data<-read.csv("~/Downloads/syria_data.csv")

model1 <- glmer.nb(fatalities~cf +fatalities_lag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model2 <- glmer.nb(fatalities~cf + prev_cf+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model3 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model4 <- glmer.nb(fatalities~cf +  prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_sl_univ+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model5 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data[data$date<"2015-01-01",],verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model6 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data[data$date>"2015-01-01",],verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

stargazer(model1,model2,model3,model4,model5,model6, type="text",df=F, digits=2,star.cutoffs=c(0.05,0.01,0.001))


# Table A2
rm(list=ls())
data_all<-read.csv("~/Downloads/syria_data_all.csv")

model1 <- glmer(fatalities~cf +fatalities_lag+(1|grid)+(1|week), data=data_all,verbose=T,family="poisson",control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model2 <- glmer(fatalities~cf + prev_cf+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all,family="poisson",verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model3 <- glmer(fatalities~cf + prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all,family="poisson",verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model4<- glmer(fatalities~cf +prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+cf_sl_univ+(1|grid)+(1|week), data=data_all,family="poisson",verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 
as.Date(data_all$date)

model5 <- glmer(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all[data_all$date<"2015-01-01",],family="poisson",verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model6 <- glmer(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data_all[data_all$date>"2015-01-01",],family="poisson",verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

stargazer(model1,model2,model3,model4,model5,model6, type="text",df=F, digits=2,star.cutoffs=c(0.05,0.01,0.001))


# Table A3

model1 <- glmer.nb(fatalities~cf +fatalities_lag+nat_cf+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model2 <- glmer.nb(fatalities~cf + prev_cf+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+nat_cf+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model3 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+nat_cf+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model4<- glmer.nb(fatalities~cf +prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+cf_sl_univ+nat_cf+(1|grid)+(1|week), data=data_all,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model5 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+nat_cf+(1|grid)+(1|week), data=data_all[data_all$date<"2015-01-01",],verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model6 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+nat_cf+(1|grid)+(1|week), data=data_all[data_all$date>"2015-01-01",],verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

stargazer(model1,model2,model3,model4,model5,model6, type="text",df=F, digits=2,star.cutoffs=c(0.05,0.01,0.001))


# Table A4
rm(list=ls()) 
data<-read.csv("~/Downloads/syria_data.csv")

model1 <- glmer.nb(fatalities~cf +fatalities_lag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model2 <- glmer.nb(fatalities~cf + prev_cf+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2_tlag+cf_splag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model3 <- glmer.nb(fatalities~cf + prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2_tlag+cf_splag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model4<- glmer.nb(fatalities~cf +prev_cf_dur_gov_new+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2_tlag+cf_splag+cf_sl_univ+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

stargazer(model1,model2,model3,model4, type="text",df=F, digits=2,star.cutoffs=c(0.05,0.01,0.001))


# Table A5

model1 <- glmer.nb(fatalities~cf + prev_cf_general+ cf:prev_cf_general+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

model2 <- glmer.nb(fatalities~cf + prev_cf_general_dur+ cf:prev_cf_general_dur+ stepwise+ humanitarian+mediation+evacuation+fatalities_lag+splag_2+cf_splag+(1|grid)+(1|week), data=data,verbose=T,control = glmerControl(calc.derivs = FALSE),nAGQ=0) 

stargazer(model1,model2, type="text",df=F, digits=2,star.cutoffs=c(0.05,0.01,0.001))

